﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.MsQuestion;
using Ccit.Utils;
using Ccit.OA.Utils;

namespace Ccit.OA.Question
{
    public partial class qst_dataentry_view : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["MSGIDS"] = "";
                _hidsRecid.Value = (null != Request.QueryString["Id"]) ? Request.QueryString["Id"].ToString() : "0"; 
                InitPage(); 
            }  
        }
        private void InitPage()
        {
            if (_hidsRecid.Value != "0")
            {
                QuestionBLL Bll = new QuestionBLL();
                LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
                MsQuestionMes msMes = Bll.GetQuestions(_hidsRecid.Value.ToString());
                if (msMes != null)
                {
                    string range = SysInterface.GetUserIdsByRangeType(msMes.rangemark, msMes.range, msMes.state);
                    string[] rangework = range.Split(',');
                    for (int j = 0; j < rangework.Length; j++)
                    {
                        if (rangework[j] == login.UserId)
                        {
                            if (msMes != null)
                            {
                                lblTitle.Text = msMes.title;
                                lblEndTime.Text = msMes.endtime;
                                lblExplain.Text = msMes.explain;
                                lblType.Text = (msMes.type == "0") ? "匿名" : "实名";
                            }
                            Create();
                        }
                    }
                }
                Bll = null;
            }
        }
        private void Create()
        {
            QuestionBLL Bll = new QuestionBLL();
            DataSet dSet = Bll.GetQuestion(_hidsRecid.Value, "0");
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            if (dSet.Tables[0].Rows.Count > 0)
            { 
                for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                {
                    DataRow dRow = dSet.Tables[0].Rows[i];
                    CreateTable(i, dRow, login.UserId);
                } 
            }
            Bll = null;
        }
        private void CreateTable(int i, DataRow dRow, string userId)
        {
            int iLine = i;
            HtmlTableRow oRow = new HtmlTableRow();
            oRow.ID = "row_" + iLine.ToString();
            oRow.Attributes["class"] = "listrow2";

            HtmlTableCell oCellM = new HtmlTableCell();
            oCellM.Align = "left";
            HtmlInputHidden hidden = new HtmlInputHidden();
            hidden.Value = dRow["Qst_A2_10"].ToString();
            if (i!=0)
                oCellM.Attributes["class"] = "pformright";
            oCellM.Attributes["width"] = "100%"; 
            oCellM.InnerHtml = "<nobr>" + (i + 1).ToString() + "、" + dRow["Qst_A2_30"].ToString() + "</nobr>";
            oCellM.Controls.Add(hidden);
            oRow.Cells.Add(oCellM);
            tbProduct.Rows.Add(oRow);

            HtmlTableRow oRow1 = new HtmlTableRow();
            QuestionBLL Bll = new QuestionBLL();
            DataSet dSetX = Bll.GetQuestion(dRow["Qst_A2_10"].ToString(), "1");
            string stype = "checkbox";
            HtmlTableCell oCellX = new HtmlTableCell();
            if (dSetX.Tables[0].Rows.Count > 0)
            {
                    int newLine = 0;
                    int flag = 0;
                    for (int j = 0; j < dSetX.Tables[0].Rows.Count; j++)
                    {
                        flag++;
                        //if (j != 0)
                        //    newLine++; 
                        DataRow dRowx = dSetX.Tables[0].Rows[j];
                        string iNum = Bll.GetAnswerMes(dRowx["Qst_A3_10"].ToString(), userId);
                        string checkInfo = (iNum != "") ? "checked" : ""; 
                        HtmlGenericControl span = new HtmlGenericControl();
                        span.InnerText = dRowx["Qst_A3_40"].ToString();
                        if (dRowx["Qst_A2_50"].ToString() == "0")
                        {
                            stype = "radio";
                            HtmlInputRadioButton rb = new HtmlInputRadioButton();
                            rb.Disabled = true;
                            rb.Attributes.Add("name", stype + "_" + iLine); 
                            rb.Attributes.Add("value", dRowx["Qst_A3_10"].ToString());
                            if (checkInfo != "")
                                rb.Attributes.Add("checked", checkInfo);
                            oCellX.Controls.Add(rb);
                        }
                        else
                        {
                            stype = "checkbox";
                            HtmlInputCheckBox cb = new HtmlInputCheckBox();
                            cb.Disabled = true;
                            cb.ID = stype + "_" + iLine; 
                            cb.Attributes.Add("value", dRowx["Qst_A3_10"].ToString());
                            if (checkInfo != "")
                                cb.Attributes.Add("checked", checkInfo);
                            oCellX.Controls.Add(cb);
                        }
                        oCellX.Controls.Add(span);
                        oCellX.Align = "left";
                        oCellX.Attributes["width"] = "100%";
                        oCellX.Attributes["class"] = "pformright";
                        if (dRowx["Qst_A2_70"].ToString() != "" && dRowx["Qst_A2_70"].ToString() != "0")
                        {
                            if (flag % int.Parse(dRowx["Qst_A2_70"].ToString())==0)
                            {
                                oRow1.Cells.Add(oCellX);
                                tbProduct.Rows.Add(oRow1);
                                oCellX = new HtmlTableCell();
                                oRow1 = new HtmlTableRow();
                                newLine = 0;
                            }
                        }
                        if (flag == dSetX.Tables[0].Rows.Count)
                        {
                            oRow1.Cells.Add(oCellX);
                            tbProduct.Rows.Add(oRow1);
                        }
                    } 
            }
            if (dRow["Qst_A2_60"].ToString() == "1")
            {
                oRow = new HtmlTableRow();
                HtmlTableCell oCellQT = new HtmlTableCell();
                oCellQT.Align = "left";
                oCellQT.Attributes["class"] = "pformright";
                oCellQT.ColSpan = dSetX.Tables[0].Rows.Count;
                oCellQT.Attributes["width"] = "100%"; 
                string Options = Bll.GetOptionsMessages(dRow["Qst_A2_10"].ToString(), userId);
                oCellQT.InnerHtml = "其它: " + Options;
                oRow.Cells.Add(oCellQT);
                tbProduct.Rows.Add(oRow);
            }
        } 
    }
}
